﻿

/* ------------------------------------------------------------------------------- */
/* TREE VIEW */
/* ------------------------------------------------------------------------------- */

function create_tree(o) {



    var $TreeElabFolders = $(o.layer);

    //    var tree_type_id = o.tree_type_id;

    $TreeElabFolders.jstree({
        // the `plugins` array allows you to configure the active plugins on this instance
        //"plugins": ["themes", "json_data", "sort", "checkbox", "ui", "crrm", "cookies", "dnd", "search", "types", "hotkeys", "contextmenu"],
        plugins: ["themes", "json_data", "sort", "ui", "crrm", "dnd", "search", "types"],
        // "themes","json_data","ui","crrm","cookies","dnd","search","types","hotkeys","contextmenu" 
        checkbox: { two_state: true },
        sort: function (a, b) { return this.get_text(a).toLowerCase() > this.get_text(b).toLowerCase() ? 1 : -1; },
        dnd: {
            drop_finish: function () {
                alert("DROP");
            },
            drag_check: function (data) {
                if (data.r.attr("id") == "node_0") {
                    //return false;
                }
                return {
                    after: false,
                    before: false,
                    inside: true
                };
            },
            drag_finish: function (data) {

                //$TreeElabFolders.jstree("check_node", data.o.parentElement.id);
                var selezionato = "";
                selezionato = data.o.parentElement.id

                var stringa = "";
                var data_type = "";
                $('[id^="ChkItem"]:checked').each(function (index) {

                    //stringa += $(this).val() + ',';
                    stringa += $(this).parent().attr("id") + ',';
                    data_type = $(this).parent().attr("data-type");
                });

                //stringa += selezionato;
                //alert(data_type);

                $.ajax({
                    url: o.webservice_url + "DndElaborations",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    //data: '{"id":"' + data.r.attr("id").replace("node_", "") + '", "type": "' + data.o.parentElement.id + '"}',
                    data: '{"id":"' + data.r.attr("id") + '", "type": "' + stringa + '", "tree_type": "' + data_type + '"}',
                    success: function (r) {
                        //alert(data.d);
                        if (!r.d.status) {
                            $.jstree.rollback(data.rlbk);
                        }
                        if (r.d.status) {
                            o.on_dnd(r);
                            //$('#BtnReload_UpPElaborations').click();
                        }
                    }
                });
            }
        },
        json_data: {

            ajax: {

                url: o.webservice_url + "GetFolders",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",

                data: function (n) {
                    if (n == -1) {
                        id = 0;
                    } else {
                        id = n.attr("id");
                    }
                    return '{"operation": "get_children","id":"' + id + '", "tree_type": "' + o.tree_type_id + '"}'

                },
                success: function (d) {
                    return d.d;
                }
            }
        },

        // Using types - most of the time this is an overkill
        // read the docs carefully to decide whether you need types
        types: {
            // I set both options to -2, as I do not need depth and children count checking
            // Those two checks may slow jstree a lot, so use only when needed
            max_depth: -2,
            max_children: -2,
            // I want only `drive` nodes to be root nodes 
            // This will prevent moving or creating any other type as a root node
            valid_children: ["drive"],
            types: {
                // The default type
                'default': {
                    // I want this type to have no children (so only leaf nodes)
                    // In my case - those are files
                    valid_children: "none",
                    // If we specify an icon for the default type it WILL OVERRIDE the theme icons
                    icon: { image: o.icons_path + "file.png" }
                },
                // The `folder` type
                folder: {
                    // can have files and other folders inside of it, but NOT `drive` nodes
                    valid_children: ["default", "folder", "folder_share"],
                    icon: { image: o.icons_path + "folder_open.png" }
                },
                folder_share: {
                    // can have files and other folders inside of it, but NOT `drive` nodes
                    valid_children: ["default", "folder", "folder_share"],
                    icon: { image: o.icons_path + "folder_open_share.png" }
                },
                // The `drive` nodes 
                drive: {
                    // can have files and folders inside, but NOT other `drive` nodes
                    valid_children: ["default", "folder", "folder_share"],
                    icon: { image: o.icons_path + "root.png" },
                    // those prevent the functions with the same name to be used on `drive` nodes
                    // internally the `before` event is used
                    start_drag: false,
                    move_node: false,
                    delete_node: false,
                    remove: false
                }
            }
        },
        // UI & core - the nodes to initially select and open will be overwritten by the cookie plugin

        // the UI plugin - it handles selecting/deselecting/hovering nodes
        ui: {
            select_limit: 1,

            // this makes the node with ID node_4 selected onload
            "initially_select": [o.root_folder_id]
        },

        // the core plugin - not many options here
        core: {
            // just open those two nodes up
            // as this is an AJAX enabled tree, both will be downloaded from the server
            "initially_open": [o.root_folder_id]
        }

    });

    $TreeElabFolders.bind("loaded.jstree", function (event, data) {
    });

    $TreeElabFolders.bind("select_node.jstree", function (e, data) {
        o.on_select_node(e, data);
        //$('#HidSelectFolderId').val(data.rslt.obj.attr("id"));
        //$('#BtnReload_UpPElaborations').click();
        //close_ajax_wait(null);

    });

    $TreeElabFolders.bind("create.jstree", function (e, data) {
        $.ajax({
            url: o.webservice_url + "CreateFolder",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: '{"operation": "create_node", "id":"' + data.rslt.parent.attr("id") + '", "position":"' + data.rslt.position + '", "title":"' + data.rslt.name + '", "type":"' + data.rslt.obj.attr("rel") + '", "tree_type": "' + o.tree_type_id + '"}',
            success: function (r) {
                //alert(r.d.status);
                if (r.d.status) {
                    $(data.rslt.obj).attr("id", r.d.id);
                }
                else {
                    $.jstree.rollback(data.rlbk);
                }
            }
        });

    });

    $TreeElabFolders.bind("open_node.jstree", function (e, data) {

        // data.inst is the instance which triggered this event
        //             data.inst.select_node("#phtml_2", true);
    });

    $TreeElabFolders.bind("rename.jstree", function (e, data) {
        $.ajax({
            url: o.webservice_url + "RenameFolder",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: '{"operation": "rename_node", "id":"' + data.rslt.obj.attr("id") + '", "title":"' + data.rslt.new_name + '", "tree_type": "' + o.tree_type_id + '"}',
            success: function (r) {
                //alert(data.d);
                if (!r.d.status) {
                    $.jstree.rollback(data.rlbk);
                }
            }
        });

    });

    $TreeElabFolders.bind("remove.jstree", function (e, data) {
        data.rslt.obj.each(function () {
            if (this.id == 'node_0') {
                alert('sono la root');
                data.inst.refresh();
            } else {
                $.ajax({
                    url: o.webservice_url + "RemoveFolder",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    data: '{"operation": "remove_node", "id":"' + this.id + '", "tree_type": "' + o.tree_type_id + '"}',
                    success: function (r) {
                        //alert(r.d.status);
                        //close_ajax_wait(null);
                        if (!r.d.status) {
                            $TreeElabFolders.jstree("deselect_all");
                            $.jstree.rollback(data.rlbk);
                            alert('La cartella contiene elementi');
                            //$TreeElabFolders.jstree("select_node",'node_28'); 
                        } else {
                            //data.inst.refresh();
                            alert('Cartella eliminata');
                        }
                    }
                });
            }

        });

    });

    $TreeElabFolders.bind("move_node.jstree", function (e, data) {
        data.rslt.o.each(function (i) {
            $.ajax({
                url: o.webservice_url + "MoveFolder",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                data: '{"operation": "move_node", "id": "' + $(this).attr("id") + '" , "ref": "' + (data.rslt.cr === -1 ? '1' : data.rslt.np.attr("id")) + '" , "position": "' + (data.rslt.cp + i) + '" , "copy": "' + (data.rslt.cy ? 1 : 0) + '", "tree_type": "' + o.tree_type_id + '"}',
                success: function (r) {
                    if (!r.d.status) {
                        $.jstree.rollback(data.rlbk);
                    }
                    else {
                        $(data.rslt.oc).attr("id", r.id);
                        if (data.rslt.cy && $(data.rslt.oc).children("UL").length) {
                            data.inst.refresh(data.inst._get_parent(data.rslt.oc));
                        }
                    }
                    //$("#analyze").click();
                }
            });
        });
    });

    return $TreeElabFolders

}



var chk_item;
function onmousedown_chk_item(o) {

    c = o.children('input:checkbox');
    chk_elab = c.prop('checked');
    c.prop('checked', true);
    //c.attr('checked', ! c.attr('checked') );
}

function onmouseup_chk_item(o) {

    c = o.children('input:checkbox');
    c.prop('checked', !chk_elab);
}



